﻿/*************************************************
 * 
 *  框架级
 *  
 *  数据库通用类 - 执行存储过程
 * 
 *  create by ternence
 * 
 *  create datetime at 2017-07-18
 *  
 *  西安佳卓信息科技有限公司 ©2017
 * 
 * ***********************************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Dapper;
using NF_Mode; 

namespace NF_IDAL
{
    public class DapperProcedure<T> where T : class
    {

        /// <summary>
        /// 执行存储过程无返回值
        /// </summary>
        /// <param name="sql">sql</param>
        /// <returns></returns>
        public static IEnumerable<dynamic> DoProcedureNoParme(string sql)
        {
            using (IDbConnection conn = DapperConn.OpenSqlConnection())
            {
                return conn.Query<dynamic>(sql);
            }
        }

        /// <summary>
        /// 执行存储过程 - 带参数
        /// </summary>
        /// <param name="ProcedureName">存储过程名称</param>
        /// <param name="dp">存储过程参数集合</param>
        /// <param name="bar">系统回执消息</param>
        /// <returns></returns>
        public static IEnumerable<T> DoProcedureNoParme(string ProcedureName, DynamicParameters dp,ref BaseAnswer bar)
        {
            IEnumerable<T> lst = new List<T>();
            try
            {
                using (IDbConnection conn = DapperConn.OpenSqlConnection())
                {
                    lst = conn.Query<T>(ProcedureName, dp, null, true, null, CommandType.StoredProcedure);
                }
            }
            catch (Exception ex)
            {
                bar.AnswerType = BaseAnswer.Answers.Error;
                bar.AnswerMeg = ex.Message;
            }
            return lst;
            
        }

        /// <summary>
        /// 执行存储过程 - 带参数
        /// </summary>
        /// <param name="ProcedureName">存储过程名称</param>
        /// <param name="dp">存储过程参数集合</param>
        /// <param name="bar">系统回执消息</param>
        /// <returns></returns>
        public static object DoProcedureNoParmeT(string ProcedureName, DynamicParameters dp, ref BaseAnswer bar)
        {
            object obj = new object();
            try
            {
                using (IDbConnection conn = DapperConn.OpenSqlConnection())
                {
                    obj = conn.Query(ProcedureName, dp, null, true, null, CommandType.StoredProcedure);
                }
            }
            catch (Exception ex)
            {
                bar.AnswerType = BaseAnswer.Answers.Error;
                bar.AnswerMeg = ex.Message;
            }
            return obj;

        }



    }
}
